package com.dropTables.TakingNote;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.sql.Timestamp;

import android.app.AlarmManager;
import android.app.PendingIntent;
public class TimeEditActivity extends Activity {
	public static  DatePicker dp;
	public static TimePicker tp;
	@Override
	protected void onStart()
	{
		super.onStart();
	}
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.time_edit);
		dp = (DatePicker) findViewById(R.id.datePicker1);
		tp = (TimePicker) findViewById(R.id.timePicker1);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) 
		{
			if(extras.getInt("hour")>0)
			{
				dp.updateDate(extras.getInt("year"), extras.getInt("month"), extras.getInt("day"));
				tp.setCurrentMinute(extras.getInt("min"));
				tp.setCurrentHour(extras.getInt("hour"));
			}
			
		}
		
		Button confirm_button = (Button) findViewById(R.id.confirm_time);
		confirm_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) 
			{
				String tStamp="";
				//Retrieve data from the date and time pickers.
				int year = dp.getYear();
				int month = dp.getMonth();
				int day = dp.getDayOfMonth();
				int hour = tp.getCurrentHour();
				int min = tp.getCurrentMinute();
				
				//Retrieve the strings from the TextEdit Activity class
				String title = NoteEditActivity.title;
				String body = NoteEditActivity.body;
				
				//if user doesn't set title and body, put a default
				if (title.equals(""))
				{
					title = "Title";
				}
				if (body.equals(""))
				{
					body = "body";
				}
				String search = NoteEditActivity.gen_loc;
				String spec_loc = NoteEditActivity.spec_loc;
				//Get the date-time formatted string.
				tStamp = String.format("%04d",year)+"-"+String.format("%02d",month+1)+"-"+String.format("%02d",day)+" "+String.format("%02d",hour)+":"+String.format("%02d",min)+":00";
				Bundle bundle = new Bundle();
				bundle.putString(ReminderDbHelper.KEY_TITLE, title);
				bundle.putString(ReminderDbHelper.KEY_BODY, body);
				bundle.putString(ReminderDbHelper.KEY_G_LOC, search);
				bundle.putString(ReminderDbHelper.KEY_S_LOC, spec_loc);
				bundle.putString(ReminderDbHelper.KEY_TIME, tStamp);
				if (NoteEditActivity.rRowId != -1) 
				{
					bundle.putLong(ReminderDbHelper.KEY_ROWID, NoteEditActivity.rRowId);
					bundle.putLong(ReminderDbHelper.KEY_R_ID,NoteEditActivity.rId);
				}
				Intent rIntent = new Intent();
				rIntent.putExtras(bundle);
				getParent().setResult(RESULT_OK, rIntent);
				finish();
			}
		});
	}

}
